package model

import (
	"context"
	sq "github.com/Masterminds/squirrel"
	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"time"
	"xialou_platform/app/requestx"
)

var _ WalletLogModel = (*customWalletLogModel)(nil)

type (
	// WalletLogModel is an interface to be customized, add more methods here,
	// and implement the added methods in customWalletLogModel.
	WalletLogModel interface {
		walletLogModel
		FindProfit(ctx context.Context, sourceId int64, startTime, endTime time.Time) int64
		FindCountBySourceId(ctx context.Context, sourceId int64) int64
		FindListCount(ctx context.Context, sourceId, Type int64) int64
		FindList(ctx context.Context, page *requestx.ListReq, sourceId, Type int64) ([]*WalletLog, error)

		CheckBillCountByBusiness(ctx context.Context, sourceId, orderId, business, t int64) (int64, error)

		FindProfitByBusiness(ctx context.Context, business, agentId int64) int64
		FindCountByBusiness(ctx context.Context, business, agentId int64) int64
	}

	customWalletLogModel struct {
		*defaultWalletLogModel
	}
)

// NewWalletLogModel returns a model for the database table.
func NewWalletLogModel(conn sqlx.SqlConn, c cache.CacheConf) WalletLogModel {
	return &customWalletLogModel{
		defaultWalletLogModel: newWalletLogModel(conn, c),
	}
}

func (m *defaultWalletLogModel) FindProfit(ctx context.Context, sourceId int64, startTime, endTime time.Time) int64 {
	selectBuilder := sq.Select("sum(amount)").From(m.table).Where(sq.Eq{"type": 1})
	if sourceId > 0 {
		selectBuilder = selectBuilder.Where(sq.Eq{"source_id": sourceId})
	}

	if startTime != endTime {
		selectBuilder = selectBuilder.Where(sq.GtOrEq{"create_time": startTime}).Where(sq.LtOrEq{"create_time": endTime})
	}

	query, args, err := selectBuilder.PlaceholderFormat(sq.Dollar).ToSql()
	if err != nil {
		return 0
	}
	var count int64
	err = m.QueryRowNoCacheCtx(ctx, &count, query, args...)
	if err != nil {
		return 0
	}
	return count

}

func (m *defaultWalletLogModel) FindCountBySourceId(ctx context.Context, sourceId int64) int64 {
	selectBuilder := sq.Select("count(1)").From(m.table).Where(sq.Eq{"type": 1})

	selectBuilder = selectBuilder.Where(sq.Eq{"source_id": sourceId})

	query, args, err := selectBuilder.PlaceholderFormat(sq.Dollar).ToSql()
	if err != nil {
		return 0
	}
	var count int64
	err = m.QueryRowNoCacheCtx(ctx, &count, query, args...)
	if err != nil {
		return 0
	}
	return count

}
func (m *defaultWalletLogModel) FindListCount(ctx context.Context, sourceId, Type int64) int64 {
	selectBuilder := sq.Select("count(1)").From(m.table).Where(sq.Eq{"source_id": sourceId})

	if Type > 0 {
		selectBuilder = selectBuilder.Where(sq.Eq{"type": Type})
	}
	query, args, err := selectBuilder.PlaceholderFormat(sq.Dollar).ToSql()
	if err != nil {
		return 0
	}
	var count int64
	err = m.QueryRowNoCacheCtx(ctx, &count, query, args...)
	if err != nil {
		return 0
	}
	return count

}
func (m *defaultWalletLogModel) FindList(ctx context.Context, page *requestx.ListReq, sourceId, Type int64) ([]*WalletLog, error) {
	selectBuilder := sq.Select(walletLogRows).From(m.table).Limit(page.Limit()).Offset(page.Offset())
	selectBuilder = selectBuilder.Where(sq.Eq{"source_id": sourceId})

	if Type > 0 {
		selectBuilder = selectBuilder.Where(sq.Eq{"type": Type})
	}
	query, args, err := selectBuilder.OrderBy("create_time desc").PlaceholderFormat(sq.Dollar).ToSql()
	if err != nil {
		return nil, err
	}
	var resp []*WalletLog
	err = m.QueryRowsNoCacheCtx(ctx, &resp, query, args...)
	return resp, err
}

// CheckBillCountByBusiness 检查记录数量
func (m *defaultWalletLogModel) CheckBillCountByBusiness(ctx context.Context, sourceId, orderId, business, t int64) (int64, error) {
	selectBuilder := sq.Select("count(1)").From(m.table).Where(sq.Eq{"type": t, "business": business})
	if sourceId > 0 {
		selectBuilder = selectBuilder.Where(sq.Eq{"source_id": sourceId})
	}
	if orderId > 0 {
		selectBuilder = selectBuilder.Where(sq.Eq{"order_id": orderId})
	}
	query, args, err := selectBuilder.PlaceholderFormat(sq.Dollar).ToSql()
	if err != nil {
		logx.Error("WalletLogModel->CheckBillCountByBusiness err : %v , in : %s", err)
		return 0, err
	}
	var count int64
	err = m.QueryRowNoCacheCtx(ctx, &count, query, args...)
	if err != nil {
		logx.Error("WalletLogModel->CheckBillCountByBusiness err : %v , in : %s", err)
		return 0, err
	}
	return count, nil
}

func (m *defaultWalletLogModel) FindProfitByBusiness(ctx context.Context, business, agentId int64) int64 {
	selectBuilder := sq.Select("sum(amount)").From(m.table).Where(sq.Eq{"type": 1})
	if business > 0 {
		selectBuilder = selectBuilder.Where(sq.Eq{"business": business})
	}
	if agentId > 0 {
		selectBuilder = selectBuilder.Where(sq.Eq{"source_id": agentId})
	}
	query, args, err := selectBuilder.PlaceholderFormat(sq.Dollar).ToSql()
	if err != nil {
		return 0
	}
	var count int64
	err = m.QueryRowNoCacheCtx(ctx, &count, query, args...)
	if err != nil {
		return 0
	}
	return count

}
func (m *defaultWalletLogModel) FindCountByBusiness(ctx context.Context, business, agentId int64) int64 {
	selectBuilder := sq.Select("count(1)").From(m.table).Where(sq.Eq{"type": 1})
	if business > 0 {
		selectBuilder = selectBuilder.Where(sq.Eq{"business": business})
	}
	if agentId > 0 {
		selectBuilder = selectBuilder.Where(sq.Eq{"source_id": agentId})
	}
	query, args, err := selectBuilder.PlaceholderFormat(sq.Dollar).ToSql()
	if err != nil {
		return 0
	}
	var count int64
	err = m.QueryRowNoCacheCtx(ctx, &count, query, args...)
	if err != nil {
		return 0
	}
	return count

}
